package com.moter.one.wyydemo1.controller;

import com.moter.one.wyydemo1.entity.UserInfo;
import com.moter.one.wyydemo1.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FindUserInfo {
  @Autowired
    UserInfoService userInfoService;
  @CrossOrigin
  @RequestMapping("/FindUserInfo")
  /**
   * 根据account找对应的用户信息
   */
    public UserInfo FindUserInfo(HttpSession session){
        String account = (String) session.getAttribute("account");
        System.out.println(account);
        List<UserInfo> list = userInfoService.searchAllUsers();//找到所有用户信息
        UserInfo us = FindUser(account,list);
        if(us==null)return null;//没找到
        else {
           System.out.println(us);
           return us;
        }
     }
     public UserInfo FindUser(String account,List<UserInfo> list){//找account对应的用户
         for(int i=0;i<list.size();i++){
             if(account.equals(list.get(i).getAccount())){
                 return list.get(i);
             }
         }
         return null;
     }

    @CrossOrigin
    @RequestMapping("/FindAllUserInfo")
    /**
     * 返回所有用户的信息---------------接口1
     */
    public List<UserInfo> FindAllUserInfo(){
        List<UserInfo> list = userInfoService.searchAllUsers();//找到所有用户信息
        if(list == null) return null;//没有用户
        else {
            //返回所有用户
            //System.out.println(list);
            return list;
        }
    }

    @CrossOrigin
    @RequestMapping("/FindAllUserInfoAccount")
    /**
     * 返回用户的所有账号---------------接口2
     */
    public List<String> FindAllUserInfoAccount(){
        List<UserInfo> list = userInfoService.searchAllUsers();//找到所有用户信息
        List<String> accounts = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            accounts.add(list.get(i).getAccount());
        }
        if(accounts.size()==0)return null;//没有账号
        else{
            //返回账户数组
            System.out.println(accounts);
            return accounts;
        }

    }


}
